package com.usian.comment.service.impl;

import com.usian.comment.feign.ArticleFeign;
import com.usian.comment.feign.UserFeign;
import com.usian.comment.service.ApCommentService;
import com.usian.model.article.pojos.ApArticle;
import com.usian.model.comment.dto.CommentDto;
import com.usian.model.comment.dto.CommentLikeDto;
import com.usian.model.comment.dto.CommentSaveDto;
import com.usian.model.comment.pojo.ApComment;
import com.usian.model.comment.pojo.ApCommentLike;
import com.usian.model.comment.vo.ApCommentVo;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import com.usian.model.user.pojos.ApUser;
import com.usian.utils.threadlocal.AppThreadLocalUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ApCommentServiceImpl implements ApCommentService {

    @Autowired
    private UserFeign userFeign;
    @Autowired
    private ArticleFeign articleFeign;
    @Autowired
    private MongoTemplate mongoTemplate;

    /**
     * 保存评论
     * @param dto
     * @return
     */
    @Override
    public ResponseResult saveComment(CommentSaveDto dto) {
        //判断参数
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断是否登录
        ApUser user = AppThreadLocalUtils.getUser();
        if (user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //根据登录用户的id查询用户
        ApUser apUser = userFeign.findByUserId(user.getId());
        if (apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN,"登录信息有误");
        }
        //封装结果
        ApComment apComment = new ApComment();
        apComment.setAuthorId(apUser.getId());
        apComment.setAuthorName(apUser.getName());
        apComment.setEntryId(dto.getArticleId());
        //查询频道id并封装
        ApArticle apArticle=articleFeign.findByArticleId(dto);
        apComment.setChannelId(apArticle.getChannelId());
        apComment.setType(0);
        apComment.setContent(dto.getContent());
        apComment.setImage(apUser.getImage());
        apComment.setLikes(0);
        apComment.setReply(0);
        apComment.setFlag(0);
        apComment.setLatitude(null);
        apComment.setLongitude(null);
        apComment.setOrd(1);
        apComment.setCreatedTime(new Date());
        apComment.setUpdatedTime(new Date());
        //执行保存
        mongoTemplate.insert(apComment);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    //给评论点赞
    @Override
    public ResponseResult likeComment(CommentLikeDto dto) {
        //检查参数
        if (dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //判断是否登录
        ApUser user = AppThreadLocalUtils.getUser();
        if (user==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN);
        }
        //根据登录用户的id查询用户
        ApUser apUser = userFeign.findByUserId(user.getId());
        if (apUser==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.NEED_LOGIN,"登录信息有误");
        }
        //根据评论的id查询
        ApComment apComment = mongoTemplate.findById(dto.getCommentId(), ApComment.class);
        if (dto.getOperation()==0){//点赞
            apComment.setLikes(apComment.getLikes()+1);//喜欢数+1
            mongoTemplate.save(apComment);
            ApCommentLike apCommentLike = new ApCommentLike();
            apCommentLike.setAuthorId(apUser.getId());
            apCommentLike.setCommentId(dto.getCommentId());
            apCommentLike.setOperation(dto.getOperation());
            apCommentLike.setCreatedTime(new Date());
            mongoTemplate.insert(apCommentLike);
        }else {//取消点赞
            if (apComment.getLikes()>0){//判断喜欢数大于0
                apComment.setLikes(apComment.getLikes()-1);//喜欢数就-1
                mongoTemplate.save(apComment);
            }
            //创建删除条件
            Query query = Query.query(Criteria.where("authorId").is(apUser.getId()).and("commentId").is(dto.getCommentId()));
            //执行删除
            mongoTemplate.remove(query,ApCommentLike.class);
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    //获取评论列表
    @Override
    public ResponseResult findAllComment(CommentDto dto) {
        //判断参数
        if (dto.getArticleId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        Integer pageSize=10;//设置查询条数
        //根据文章id将评论全部查询出来
        Query query = Query.query(Criteria.where("entryId").is(dto.getArticleId()));
        query.limit(pageSize);
        List<ApComment> apComments = mongoTemplate.find(query,ApComment.class);
        //判断是否登录
        ApUser user = AppThreadLocalUtils.getUser();
        if (user==null){//未登录展示的数据
            return ResponseResult.okResult(apComments);
        }else {//登录后展示的数据
            ArrayList<String> commentIdList = new ArrayList<>();
            for (ApComment apComment : apComments) {//遍历获取所有评论的id并存入集合
                commentIdList.add(apComment.getId());
            }
            //获取所有点过赞的评论
            Query query1 = Query.query(Criteria.where("commentId").in(commentIdList).and("authorId").is(user.getId()));
            List<ApCommentLike> apCommentLikes = mongoTemplate.find(query1, ApCommentLike.class);

            ArrayList<ApCommentVo> apCommentVos = new ArrayList<>();//创建一个存储需要展示评论的集合
            if (apCommentLikes.size()>0){//有点赞的评论
                for (ApComment apComment : apComments) {//遍历所有评论的集合
                    ApCommentVo apCommentVo = new ApCommentVo();//创建一个展示信息的对象
                    BeanUtils.copyProperties(apComment,apCommentVo);//将评论的数据复制到需要展示信息的里面
                    for (ApCommentLike apCommentLike : apCommentLikes) {//遍历点赞信息的集合
                        if (apComment.getId().equals(apCommentLike.getCommentId())){//判断所有的评论中有没有点赞的
                            apCommentVo.setOperation(0);//有的话就设置状态，0为点赞
                            break;
                        }
                    }
                    apCommentVos.add(apCommentVo);//将需要展示的对象存到集合中
                }
                return ResponseResult.okResult(apCommentVos);
            }else {//没有点赞的评论
                return ResponseResult.okResult(apComments);
            }
        }
    }


}
